package com.lanou.moving.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.moving.R;
import com.lanou.moving.activity.MVPlayActivity;
import com.lanou.moving.adapter.MVDaylyAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.MVDaylyRoot;
import com.lanou.moving.utils.SwipeRefreshLoadingLayout;
import com.lanou.moving.volley.GsonRequest;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;

/**
 * Created by 梁树珍 on 15/11/3.
 */
public class MVDaylyFragment extends BaseFragment implements SwipeRefreshLoadingLayout.OnRefreshListener, SwipeRefreshLoadingLayout.OnLoadListener, AdapterView.OnItemClickListener {
    private SwipeRefreshLoadingLayout mv_dayly_wipeRefresh;
    private ListView mv_dayly_listView;
    private List<MVDaylyRoot.DataEntity> entityList;
    private MVDaylyAdapter daylyAdapter;
    private int page = 1;
    private VolleySingleton volleySingleton;
    private String url;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mv_dayly, null);
        volleySingleton = VolleySingleton.getInstance(mContext);
        mv_dayly_listView = (ListView) view.findViewById(R.id.mv_dayly_listView);
        mv_dayly_listView.setOnItemClickListener(this);
        mv_dayly_wipeRefresh = (SwipeRefreshLoadingLayout) view.findViewById(R.id.mv_dayly_wipeRefresh);
        getDatas();
        init();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getDatas();
        init();
    }

    private void init(){
        mv_dayly_wipeRefresh.setOnRefreshListener(this);
        mv_dayly_wipeRefresh.setOnLoadListener(this);
        mv_dayly_wipeRefresh.setTopColor(R.color.transparent,
                R.color.transparent, R.color.transparent, R.color.transparent);
        mv_dayly_wipeRefresh.setBottomColor(R.color.theme, R.color.white,
                R.color.theme, R.color.white);
    }

    private void getDatas(){
        url = URL.URL_LATEST_MV + page;
        GsonRequest<MVDaylyRoot> request = new GsonRequest<MVDaylyRoot>(url, MVDaylyRoot.class, new Response.Listener<MVDaylyRoot>() {
            @Override
            public void onResponse(MVDaylyRoot response) {
                entityList = response.getData();
                daylyAdapter = new MVDaylyAdapter(getActivity(), entityList);
                mv_dayly_listView.setAdapter(daylyAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onRefresh() {
        mv_dayly_wipeRefresh.setLoading(false);
    }

    @Override
    public void onLoad() {
        page++;
        url = URL.URL_LATEST_MV + page;
        GsonRequest<MVDaylyRoot> request = new GsonRequest<MVDaylyRoot>(url, MVDaylyRoot.class, new Response.Listener<MVDaylyRoot>() {
            @Override
            public void onResponse(MVDaylyRoot response) {
                mv_dayly_wipeRefresh.setLoading(false);
                entityList = response.getData();
                daylyAdapter.addDatas(entityList);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), MVPlayActivity.class);
        intent.putExtra("videoId", entityList.get(position).getMvList().get(0).getVideoId());
        startActivity(intent);
    }
}
